<%/**
 * benchmarks.jsp
 * 
 * Allows the user to view a list of data on their PR's.
 *
 * @author Chris Avery
 */%>
	
	<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="b1w1nn1ng.crossf1t.db.*"%>
    <%@page import="b1w1nn1ng.crossf1t.gui.*"%>
    <%@page import="b1w1nn1ng.crossf1t.business.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Benchmarks</title>
<jsp:include page="/detectBrowser.jsp" />	
</head>
 <%
 	/* this is a convienience variable:
 	 * 0: not ready to display anything
 	 * 1: ready to display a primary
 	 * 2: ready to display a secondary
 	 */
 	int ready = 0;
 	
 %>
<body>
	<div id="container">
		<jsp:include page="/navbar.jsp" />
		<h1> Benchmarks </h1> <hr />
		Select a workout type:
	<form action="./benchmarks.jsp" method="get">
	<select name="primary" onchange="this.form.submit();"> 
		<%
 		//get the different values and add them as select options
 		String[] primaries = Database.getPrimarySeeList(false);

 		//Print the "Categories" option at the top of the menu if we haven't selected one yet
 		if (request.getParameter("primary") == null) {
 			out.println("<option>Categories</option>");
 		}

 		//for each entry in the string array, prints it as an option in format:
 		// <option value="entry">entry</option>
 		for (String entry : primaries) {

 			// If an option was selected last time, we do this check to keep it selected:
 			if (request.getParameter("primary") != null) {
 				if (request.getParameter("primary").equals(entry))
 					out.println("<option selected value=\"" + entry + "\">"
 							+ entry + "</option>");
 				else
 					out.println("<option value=\"" + entry + "\">" + entry
 							+ "</option>");
 				// Else, just print the options
 			} else {
 				out.println("<option value=\"" + entry + "\">" + entry
 						+ "</option>");
 			}
 		}
 		%>
	</select>
	<%
	// If a primary category was selected, we may have to display the secondary menu. Check
	// if we have to here:
	if (request.getParameter("primary") != null) {
		String[] secondary = Database.getSecondarySeeList(request
				.getParameter("primary"));

		//if the secondary seeList is null, we're done, there is no secondary.
		if (secondary == null) {
			ready = 1;
		} else {
			//else, there is a secondary: here's the select menu for it.
			out.println("<select name=\"secondary\" onchange=\"this.form.submit();\">");
			//Print the "Excercise" option at the top of the menu if we haven't selected one yet
			//OR if primary changed since last time.
			if (request.getParameter("secondary") == null) {
				out.println("<option>Excercise</option>");
				ready = 0;
			} else {
				boolean found = false;
				//check if primary changed by seeing if selected secondary is in the list still
				for (int i = 0; i < secondary.length; i++) {
					if (secondary[i].equals(request
							.getParameter("secondary"))) {
						found = true;
						ready = 2;
						//TODO: Possible bug, if changing the primary menu, the secondary will be selected
						// anyway. Can't test until database is there.
						break;
					}
				}
				if (!found) {
					out.println("<option>Excercise</option>");
					ready = 0;
				}
			}

			//for each entry in the string array, prints it as an option in format:
			// <option value="entry">entry</option>
			for (String entry : secondary) {

				// If an option was selected last time, we do this check to keep it selected:
				if (request.getParameter("secondary") != null) {
					if (request.getParameter("secondary").equals(entry))
						out.println("<option selected value=\"" + entry
								+ "\">" + entry + "</option>");
					else
						out.println("<option value=\"" + entry + "\">"
								+ entry + "</option>");
					// Else, just print the options
				} else {
					out.println("<option value=\"" + entry + "\">"
							+ entry + "</option>");
				}
			}
			out.println("</select>");
		}
	}
	%>		
	</form>
		
	<!-- 
	*************************************************************
	OUTPUT TABLE STARTS HERE 
	*************************************************************
	-->
	<% //Used in painting the table alternating colors per row
	boolean odd = true; 
	
	//Not ready to output
	if(ready == 0) {		
		//display a dummy table, request that the user input an excercise to search by.
		%>
		<!-- This first table is the column headers -->
		<table class="datatableheader">
			<tr>
				<td>Please select a value from the menu above.</td>
			</tr>
		</table>
		<!-- The second table is wrapped in a div to allow it to be scrollable -->
		<div class="datatableframe"></div>
		<%
	}
	//Ready to output a primary
	if (ready == 1) {
		//If the user has selected something which can be pulled from the database, now we do so.
		UserStat[] userStats = Database.getCompletePRbyStat(request.getParameter("primary"));
		%>					
		<!-- This first table is the column headers -->
		<table class="datatableheader">
			<tr>
				<td width="50%">Date</td>
				<td width="50%"><%= request.getParameter("primary") %></td>
			</tr>
		</table>
		<!-- The second table is wrapped in a div to allow it to be scrollable -->
		<div class="datatableframe">
			<table class="datatable">
			<% 
			for(int i = 0; i < userStats.length; i++) {
				//Every odd row is displayed with a different CSS class to give a pretty effect.
				if(odd) {
					odd = false;
					out.println("<tr class = \"datatablealt\">");
				} else {
					odd = true;
					out.println("<tr>");
				}
				out.println("<td width=\"50%\">"+userStats[i].getFormattedDate()+"</td>");
				out.println("<td width=\"50%\">"+userStats[i].getValue()+"</td>");
			}
			 %>
			</table>
		</div>
	<% 
	}  //end if(ready == 1)
	//Ready to output a secondary	
	if(ready == 2) {
		Exercise[] exercises = Database.getCompletePRbyExercise(request.getParameter("secondary"));
		%>
		<!-- This first table is the column headers -->
		<table class="datatableheader">
			<tr>
				<td width="25%">Date</td>
				<td width="25%">Weight</td>
				<td width="25%">Reps</td>
				<td width="25%">Time</td>
			</tr>
		</table>
		<!-- The second table is wrapped in a div to allow it to be scrollable -->
		<div class="datatableframe">
			<table class="datatable">
			<% 
			for(int i = 0; i < exercises.length; i++) {
				//Every odd row is displayed with a different CSS class to give a pretty effect.
				if(odd) {
					odd = false;
					out.println("<tr class = \"datatablealt\">");
				} else {
					odd = true;
					out.println("<tr>");
				}
				out.println("<td width=\"25%\">"+exercises[i].getFormattedDate()+"</td>");
				if(exercises[i].getWeights() >= 0)
					out.println("<td width=\"25%\">"+exercises[i].getWeights()+"</td>");
				else
					out.println("<td width=\"25%\"></td>");
				if(exercises[i].getReps() >= 0)
					out.println("<td width=\"25%\">"+exercises[i].getReps()+"</td>");
				else
					out.println("<td width=\"25%\"></td>");
				if(exercises[i].getTime() >= 0)
					out.println("<td width=\"25%\">"+exercises[i].getTime()+"</td>");
				else
					out.println("<td width=\"25%\"></td>");
			}
			%>
			</table>
		</div>
		<%
	} //end if(ready == 2)
	%>
	</div>
</body>
</html>